package cn.edu.scau.cmi.ema.dao;

import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.dao.DataAccessException;

import org.springframework.stereotype.Repository;

import org.springframework.transaction.annotation.Transactional;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.Session;


import cn.edu.scau.cmi.ema.domain.*;
import cn.edu.scau.cmi.ema.domain.base.CmiPagination;


import cn.edu.scau.cmi.ema.util.CmiSqlUtil;

@Repository("CrawlerDAO")
@Transactional
public class CrawlerDAOImpl extends AbstractJpaDao<Crawler> implements CrawlerDAO {

	private final static Set<Class<?>> dataTypes = new HashSet<Class<?>>(
			Arrays.asList(new Class<?>[] { Crawler.class }));

	@PersistenceContext(unitName = "ema")
	private EntityManager entityManager;

	public CrawlerDAOImpl() {
		super();
	}

	public EntityManager getEntityManager() {
		return entityManager;
	}

	public Set<Class<?>> getTypes() {
		return dataTypes;
	}

	@Transactional
	public Crawler findCrawlerById(Integer id) throws DataAccessException {

		return findCrawlerById(id, -1, -1);
	}

	@Transactional
	public Crawler findCrawlerById(Integer id, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findCrawlerById", startResult, maxRows, id);
			return (Crawler) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Crawler> findCrawlerByName(String name) throws DataAccessException {

		return findCrawlerByName(name, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Crawler> findCrawlerByName(String name, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findCrawlerByName", startResult, maxRows, name);
		return new LinkedHashSet<Crawler>(query.getResultList());
	}

	@Transactional
	public Set<Crawler> findCrawlerByNameContaining(String name) throws DataAccessException {

		return findCrawlerByNameContaining(name, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Crawler> findCrawlerByNameContaining(String name, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findCrawlerByNameContaining", startResult, maxRows, name);
		return new LinkedHashSet<Crawler>(query.getResultList());
	}

	@Transactional
	public Set<Crawler> findCrawlerByUrl(String url) throws DataAccessException {

		return findCrawlerByUrl(url, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Crawler> findCrawlerByUrl(String url, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findCrawlerByUrl", startResult, maxRows, url);
		return new LinkedHashSet<Crawler>(query.getResultList());
	}

	@Transactional
	public Set<Crawler> findCrawlerByUrlContaining(String url) throws DataAccessException {

		return findCrawlerByUrlContaining(url, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Crawler> findCrawlerByUrlContaining(String url, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findCrawlerByUrlContaining", startResult, maxRows, url);
		return new LinkedHashSet<Crawler>(query.getResultList());
	}

	@Transactional
	public Set<Crawler> findCrawlerBySnapshot(String snapshot) throws DataAccessException {

		return findCrawlerBySnapshot(snapshot, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Crawler> findCrawlerBySnapshot(String snapshot, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findCrawlerBySnapshot", startResult, maxRows, snapshot);
		return new LinkedHashSet<Crawler>(query.getResultList());
	}

	@Transactional
	public Set<Crawler> findCrawlerBySnapshotContaining(String snapshot) throws DataAccessException {

		return findCrawlerBySnapshotContaining(snapshot, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Crawler> findCrawlerBySnapshotContaining(String snapshot, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findCrawlerBySnapshotContaining", startResult, maxRows, snapshot);
		return new LinkedHashSet<Crawler>(query.getResultList());
	}

	@Transactional
	public Set<Crawler> findCrawlerByDescription(String description) throws DataAccessException {

		return findCrawlerByDescription(description, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Crawler> findCrawlerByDescription(String description, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findCrawlerByDescription", startResult, maxRows, description);
		return new LinkedHashSet<Crawler>(query.getResultList());
	}

	@Transactional
	public Set<Crawler> findCrawlerByDescriptionContaining(String description) throws DataAccessException {

		return findCrawlerByDescriptionContaining(description, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Crawler> findCrawlerByDescriptionContaining(String description, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findCrawlerByDescriptionContaining", startResult, maxRows, description);
		return new LinkedHashSet<Crawler>(query.getResultList());
	}

	@Transactional
	public Crawler findCrawlerByPrimaryKey(Integer id) throws DataAccessException {
		return findCrawlerByPrimaryKey(id, -1, -1);
	}

	@Transactional
	public Crawler findCrawlerByPrimaryKey(Integer id, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findCrawlerByPrimaryKey", startResult, maxRows, id);
			return (Crawler) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Crawler> findCrawlersByIds(Set<Integer> ids) throws DataAccessException {
		try {
			Query query = createNamedQuery("findCrawlersByIds", -1, -1, ids);
			return new HashSet(query.getResultList());
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Crawler> findAllCrawlers() throws DataAccessException {

		return findAllCrawlers(-1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Crawler> findAllCrawlers(int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findAllCrawlers", startResult, maxRows);
		return new LinkedHashSet<Crawler>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Crawler> findCrawlerByCompositeProperities(String sql, int startResult, int maxRows) {
		return new LinkedHashSet<Crawler>(createQuery(sql, startResult, maxRows).getResultList());
	}

	@Transactional
	public Set<Crawler> findCrawlersByDetachedCriteria(CmiPagination page) throws DataAccessException {
		DetachedCriteria detachedCriteria = CmiSqlUtil.createDetachedCriteria(page, Crawler.class);
		Session session = entityManager.unwrap(Session.class);
		Set<Crawler> crawlers = new HashSet(detachedCriteria.getExecutableCriteria(session)
				.setFirstResult(page.getSearchBeginCursor()).setMaxResults(page.getPaginationSize()).list());

		return crawlers;
	}

	@Transactional
	public Crawler findCrawlerByPhysicalPrimaryKey(String name) throws DataAccessException {
		return findCrawlerByPhysicalPrimaryKey(name, -1, -1);
	}

	@Transactional
	public Crawler findCrawlerByPhysicalPrimaryKey(String name, int startResult, int maxRows)
			throws DataAccessException {
		try {
			Query query = createNamedQuery("findCrawlerByPhyscialPrimaryKey", startResult, maxRows, name);
			return (Crawler) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	public boolean canBeMerged(Crawler entity) {
		return true;
	}
}
